package com.hatic.process;

import java.util.Map;

import org.jxstar.dao.DaoParam;
import org.jxstar.service.BusinessObject;
import org.jxstar.service.define.DefineDataManger;
import org.jxstar.util.StringUtil;

/**
 * 处理项目监控的数据查询。
 *
 * @author TonyTan
 * @version 1.0, 2011-11-18
 */
public class ProjectMonitorBO extends BusinessObject {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 查询附件
	 * @param funIds
	 * @param fileFields
	 * @param projectId
	 * @return
	 */
	public String queryFile(String funIds, String fileFields, String projectId) {
		String[] fids = funIds.split(";");
		String[] fields = fileFields.split(";");

		StringBuilder sbjson = new StringBuilder();
		for (int i = 0, n = fids.length; i < n; i++) {
			if (fids[0].length() == 0) continue;
			sbjson.append(queryFileName(fids[i], fields[i], projectId));
		}
		if (sbjson.length() > 0) {
			String json = "[" + sbjson.substring(0, sbjson.length()-1) + "]";
			setReturnData(json);
		} else {
			setReturnData("[]");
		}
		
		return _returnSuccess;
	}
	
	//取主键值与附件字段值
	private String queryFileName(String funId, String fileField, String projectId) {
		DefineDataManger manger = DefineDataManger.getInstance();
		Map<String,String> mpFun = manger.getFunData(funId);
		
		String[] fields = fileField.split("__");
		if (fields.length > 1) fileField = fields[1];
		
		String pk_col = mpFun.get("pk_col");
		pk_col = StringUtil.getNoTableCol(pk_col);
		String auditcol = mpFun.get("audit_col");
		String from_sql = mpFun.get("from_sql");
		String where_sql = mpFun.get("where_sql");
		
		String sql = "select " + pk_col + ", " + fileField + ", " + auditcol + " " + 
		from_sql + " where " + where_sql;
		if (where_sql.length() > 0) sql += " and ";
		sql += "project_id = ?";
		
		_log.showDebug("...........query file where_sql=" + sql);
		_log.showDebug("...........query file project_id=" + projectId);
		
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(projectId);
		
		Map<String,String> mpData = _dao.queryMap(param);
		if (mpData.isEmpty()) {
			return "";
		} {
			auditcol = StringUtil.getNoTableCol(auditcol);
			return "{funid:'"+ funId +"', keyid:'" + mpData.get(pk_col) + "', audit:'"+ 
			mpData.get(auditcol) +"', filename:'" + mpData.get(fileField) + "'},";
		}
	}

	/**
	 * 查询相关功能中是否有指定项目的记录。
	 * @param projectId
	 * @return
	 */
	public String queryMonitor(String projectId) {
		StringBuilder jsonData = new StringBuilder();
		
		String createFunId = queryCreateFun(projectId);
		jsonData.append("{createFunId:'" + createFunId + "',");
		jsonData.append("project_create:" + queryFunData(createFunId, projectId) + ",");
		jsonData.append("project_process:" + queryFunData("project_process", projectId) + ",");
		jsonData.append("project_designchg:" + queryFunData("project_designchg", projectId) + ",");
		jsonData.append("project_check:" + queryFunData("project_check", projectId) + ",");
		
		jsonData.append("sj_contract_app:" + queryFunData("sj_contract_app", projectId) + ",");
		jsonData.append("sj_contract_get:" + queryFunData("sj_contract_get", projectId) + ",");
		jsonData.append("sj_balance:" + queryFunData("sj_balance", projectId) + ",");
		jsonData.append("sj_balance_get:" + queryFunData("sj_balance_get", projectId) + ",");
		
		jsonData.append("pur_apply:" + queryFunData("pur_apply", projectId) + ",");
		jsonData.append("contract_sign:" + queryFunData("contract_sign", projectId) + ",");
		jsonData.append("pay_plan:" + queryFunData("pay_plan", projectId) + ",");
		jsonData.append("contract_bill:" + queryFunData("contract_bill", projectId) + ",");
		jsonData.append("project_mplan:'" + queryPlanId(projectId) + "'}");
		
		_log.showDebug("--------jsonData=" + jsonData.toString());
		setReturnData(jsonData.toString());
		
		return _returnSuccess;
	}
	
	/**
	 * 查询有数据条数
	 * @param funId -- 功能ID
	 * @param projectId -- 项目ID
	 * @return
	 */
	private int queryFunData(String funId, String projectId) {
		DefineDataManger manger = DefineDataManger.getInstance();
		Map<String,String> mpFun = manger.getFunData(funId);
		
		String from_sql = mpFun.get("from_sql");
		String where_sql = mpFun.get("where_sql");
		
		if (from_sql.length() == 0) {
			_log.showDebug("from_sql or where_sql is null!");
			return 0;
		}
		
		String sql = "select count(*) as cnt " + from_sql + " where " + where_sql;
		if (where_sql.length() > 0) sql += " and ";
		sql += "project_id = ?";
		
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(projectId);
		
		Map<String,String> mpCnt = _dao.queryMap(param);
		return Integer.parseInt(mpCnt.get("cnt"));
	}
	
	//查询工作计划ID
	private String queryPlanId(String projectId) {
		String sql = "select mplan_id from project_mplan where auditing = '1' and project_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(projectId);
		Map<String,String> mpData = _dao.queryMap(param);
		if (mpData.isEmpty()) return "";
		
		return mpData.get("mplan_id");
	}
	
	//取项目立项的功能ID
	private String queryCreateFun(String projectId) {
		String sql = "select auth_type from project_base where project_id = ?";
		DaoParam param = _dao.createParam(sql);
		param.addStringValue(projectId);
		Map<String,String> mpData = _dao.queryMap(param);
		if (mpData.isEmpty()) {
			return "project_create";
		}
		
		String authType = mpData.get("auth_type");
		if (authType.equals("bf")) {
			return "project_create2";
		} else if (authType.equals("wq")) {
			return "project_create3";
		} else {
			return "project_create";
		}
	}
}
